# -*- coding: utf-8 -*-
import subprocess
import sys
import os
from threading import Thread
class TSendSms(Thread):
    
    def __init__(self,sn,sm,rn,rm,state,num,taskno,companyname):
        Thread.__init__(self)
        self.sn=sn
        self.sm=sm
	self.rn=rn
        self.rm=rm

        self.state=state
        self.num=num
        self.taskno=taskno
        self.companyname=companyname
        
            

    def run(self):
        print "run"
	print "self.state is %s"%(self.state)
        if self.state == "2":
            self.number = self.rm
            self.sms = '您好，%s委托%s为您递送运单号%s共%s件货物，请保留信息以便投递时核验，多谢！'%(self.sn,self.companyname,self.taskno,self.num)
        elif self.state == "6":
            self.number = self.sm            
            self.sms = '您好，%s快递已将运单号%s共%s件的货物递送给%s，非常感谢您的支持'%(self.companyname,self.taskno,self.num,self.rn)
        
        if self.state == "2" or self.state == "6":        
            print "the number is %s"%(self.number)
	    print "the sms is %s"%(self.sms)
	    ipPipe = subprocess.Popen(('/home/harold/emsharold/server/unittest/sdk4/SendSMS',self.number,self.sms), stdout=subprocess.PIPE)
            ipPipe.wait() 
        
            ipStr = ipPipe.stdout.read()
            print ipStr
            print "return code",ipPipe.returncode 
        
def main(*argv):
    if argv:
        argv = list(argv)
    else:
        argv = sys.argv[1:]    

    sn = argv[0] #the state
    sm = argv[1]
    rn = argv[2]
    rm = argv[3]
    state = argv[4]
    num = argv[5]
    taskno = argv[6]
    companyname = argv[7]
    
    fd=open("/postgresql_tmp/1.txt","a+")
#    fd=open("1.txt","wr")
    fd.write('\n')

    fd.write(sn)
    fd.write('\t')
    fd.write(sm)
    fd.write('\t')

    fd.write(rn)
    fd.write('\t')

    fd.write(rm)
    fd.write('\t')

    fd.write(state)
    fd.write('\t')

    fd.write(num)
    fd.write('\t')

    fd.write(taskno)
    fd.write('\t')

    fd.write(companyname)
    fd.write('\t')

    fd.close()
    th = TSendSms(sn,sm,rn,rm,state,num,taskno,companyname)
    th.start()
if __name__ == "__main__":
    main()
    
